package com.itheima.thread.threadlocal;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-08 09:17
 **/
public class ThreadLocalTest {
    private static ThreadLocal<String> threadLocal =new ThreadLocal<String>();

    public static void main(String[] args) {
//        创建第一个线程
        Thread threadA = new Thread(() -> {
            threadLocal.set("ThreadA:" + Thread.currentThread().getName());
            System.out.println("线程A的本地变量中的值为：" + threadLocal.get());
            System.err.println(Thread.currentThread());
            threadLocal.remove();
            System.out.println("线程A删除的本地变量后，其中的值为：" + threadLocal.get());
        });

//        创建第二个线程
        Thread threadB = new Thread(() -> {
            threadLocal.set("ThreadB:" + Thread.currentThread().getName());
            System.out.println("线程B的本地变量中的值为：" + threadLocal.get());
            System.out.println("线程B没有删除本地变量，其中的值为：" + threadLocal.get());

        });
//        启动线程A和线程B
        threadA.start();
        threadB.start();
    }
}
